package com.xy.order.aspet;

import com.xy.order.anotation.MyAnotion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class MyAspect {

    @Pointcut("@annotation(com.xy.order.anotation.MyAnotion)")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {

        /**
         * 常用：
         *     //获取参数
         *     Object[] args = joinPoint.getArgs();
         *     //获取参数名
         *     String[] aaa = ((CodeSignature)pjp.getSignature()).getParameterNames();
         * ​
         * 其他
         *     1.获取的实体类
         *     Object target = point.getTarget();
         *     2.获取的方法名称
         *     String methodName = point.getSignature().getName();
         *     3.获取的方法参数
         *     Object[] args = point.getArgs();
         *     4.获取的参数类型
         *     Class[] parameterTypes = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();
         *     5.通过反射获得拦截的method
         *     Method m = target.getClass().getMethod(methodName, parameterTypes);
         *     6.通过反射执行目标对象的连接点的方法
         *     Object o = point.proceed();
         *     7.通过反射执行目标对象连接点处的方法，使用新的参数列表替换原来的：
         *     point.proceed(Object[] args);
         * ————————————————
         * 版权声明：本文为CSDN博主「Liu-1998」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
         * 原文链接：https://blog.csdn.net/m0_46192647/article/details/125768831
         */
        System.out.println("开启环绕切面");
        Object proceed = null;
        try {
            Signature signature = proceedingJoinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature)signature;
            Method method =  methodSignature.getMethod();
            MyAnotion myAnotion = method.getAnnotation(MyAnotion.class);
            System.out.println(myAnotion.name());
            proceed = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;

    }

}
